/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sUtils;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import java.io.*;
import java.util.*;

public class Files  {

    private static final String MEGA_ROOT = "/";
    private static final char SEP = '/';
    public String path = "";
    public Vector files = null;

    public Files( String _path )
    {
        path = _path;
    }

    public int fileCount() { return files.size(); }
    public String file( int i ) { return (String)files.elementAt(i); }

    public int collectFiles( String _path, boolean subdir )
    {
        int ret=0;
        if (_path==null) _path=MEGA_ROOT;
        if (files==null) files = new Vector(1024,512);
        Enumeration e;
        FileConnection currDir = null;
        //files.addElement( new String(_path) ); ret++;
        try {
            if (MEGA_ROOT.equals(_path)) {
                e = FileSystemRegistry.listRoots ();
            }
            else {
                currDir = (FileConnection) Connector.open ("file:///" + _path);
                e = currDir.list("*",true);
            }

            while (e.hasMoreElements ()) {
                String fileName = (String) e.nextElement ();

                if (fileName.charAt (fileName.length () - 1) == SEP) {
                    // This is directory

                    if (subdir) ret += collectFiles( (_path.length()>1 ? _path : "")+fileName, subdir);
                }
                else {
                    // this is regular file
                    files.addElement( new String(_path+fileName) ); ret++;
                }
            }
            if (currDir != null) currDir.close ();
            
        } catch (IOException ioe) {}
        return ret;
    }



}
